home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / Reich-o-Matic / RandomIzer.m < prev    next >
Encoding:
Text File  |  1992-07-31  |  1.5 KB  |  83 lines

  1. #import <musickit/musickit.h>
  2. #import "RandomIzer.h"
  3.  
  4. @implementation RandomIzer:Object
  5.  
  6. static double ranNum(id self)
  7.     /* Returns a random number between 0 and 1. */
  8. {
  9. #define   RANDOMMAX (double)((long)MAXINT)
  10.  
  11.     double newVal;
  12.     newVal =  ((double)random()) / RANDOMMAX;
  13.     
  14.     return newVal;
  15. }
  16.  
  17. static int ranInt(id self,int lowBound,int highBound)
  18. /* Returns a random int between the specified bounds (inclusive) */
  19. {
  20.     return ( ranNum(self) * (highBound - lowBound) + lowBound + .5);
  21. }
  22.  
  23. static int initRan(void)
  24.     /* Initialize random numbers with a random seed based on the time of day */
  25. {
  26. #   import <sys/time.h>
  27. #define STATESIZEINBYTES 256
  28.     struct timeval tp;
  29.     unsigned seed;
  30.     gettimeofday(&tp,NULL);
  31.     seed = tp.tv_usec;
  32.     srandom(seed);
  33.  
  34.     return (seed);
  35. }
  36.  
  37. -setit
  38.   /* This method should be invoked after a new instance is created. */
  39. {
  40.     initRan(); /* Initialize random number sequence */
  41.  
  42.     return self;
  43. }
  44.  
  45. -(double)GetNumber
  46. {
  47.     return(ranNum(self));
  48. }
  49.  
  50. -(double)GetNumber:(double)scaler
  51. {
  52.     return((ranNum(self)) * scaler);
  53. }
  54.  
  55. -(double)GetNumberRangeHi:(double)hi Lo:(double)lo
  56. {
  57.     return( (ranNum(self) * (hi-lo)) + lo );
  58. }
  59.  
  60. -(double)GetPlusMinus
  61. {
  62.     return( (ranNum(self) * 2.0) - 1.0 );
  63. }
  64.  
  65. -(double)GetPlusMinus:(double)scaler
  66. {
  67.     return( (ranNum(self) * (2.0 * scaler)) - (scaler/2.0) );
  68. }
  69.  
  70. -(int)GetIndex:(int)scaler
  71. {
  72.     return(ranInt(self,0,scaler));
  73. }
  74.  
  75. -(int)GetIndexRangeHi:(int)hi Lo:(int)lo
  76. {
  77.     return(ranInt(self,lo,hi));
  78. }
  79.  
  80. @end
  81.  
  82.  
  83.